package com.leha.mp.api.controller;

import com.aliyun.oss.internal.OSSUtils;
import com.leha.mp.core.Utils.COSUtil;
import com.leha.mp.core.Utils.FileInfo;
import com.leha.mp.core.Utils.JFImgUtils;
import com.leha.mp.core.Utils.SmsUtil;
import com.leha.mp.core.services.*;
import constants.APIErrorDict;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.APIResult;

@RestController
@RequestMapping("/internal")
public class InternalController {
    @Autowired
    WxMenuServices menuServices;
    @Autowired
    HidingCallServices hidingCallServices;
    @Autowired
    WxMPServices mpService;
    @Autowired
    WxMPQrCodeServices qrCodeServices;
    @Autowired
    UtilsHelper utilsHelper;

    @GetMapping(value = "/updateMenu")
    @ApiOperation(value="更新公众号菜单_后台接口")
    APIResult updateMenu() {
        if(menuServices.updateWxMenu()) {
            return new APIResult("OK");
        } else {
            return new APIResult("Faild");
        }
    }

    @GetMapping(value = "/genCode")
    @ApiOperation(value="生成医生二维码")
    APIResult genCode(Integer doctorId) {
        return new APIResult(qrCodeServices.generateDoctorCode(doctorId));
    }

    @PostMapping(value="/call")
    APIResult makeCall(String from, String to, Integer limit_s, String callbackUrl, String data) {
       return new APIResult(hidingCallServices.MakeDoubleSideCall(from, to, limit_s, callbackUrl, data));
    }

    @DeleteMapping(value="/call/{callSid}")
    APIResult hangupCall(@PathVariable String callSid) {
        return new APIResult(hidingCallServices.HanupCall(callSid));
    }

    @PostMapping(value="/sms")
    APIResult sendSMS(String phone, String msg) {
        return new APIResult(SmsUtil.send(phone, msg));
    }

    @PostMapping(value="/uploadimg")
    @ApiOperation(value="保存图片_医生端，用户端，后台接口")
    APIResult uploadImgs(MultipartFile file, Integer userId, Integer doctorId ) {
        try {
            FileInfo ret = JFImgUtils.uploadImgs(file);
            if(userId!=null) {
                utilsHelper.SaveFile(userId, ret.getUrl(), 1, true);
            } else if(doctorId !=null) {
                utilsHelper.SaveFile(doctorId, ret.getUrl(), 1, false);
            }
            return new APIResult(ret);
        } catch (Exception e) {
            e.printStackTrace();
            return new APIResult(APIErrorDict.JFIMGError);
        }
    }
    @PostMapping(value="/saveFile")
    @ApiOperation(value="更新公众号菜单_医生端，用户端，后台接口")
    APIResult uploadFile(MultipartFile file, Integer userId, Integer doctorId) {
        FileInfo ret = COSUtil.putFile(file);
        if(userId!=null) {
            utilsHelper.SaveFile(userId, ret.getUrl(), 2, true);
        } else if(doctorId !=null) {
            utilsHelper.SaveFile(doctorId, ret.getUrl(), 2, false);
        }
        return  new APIResult(ret);
    }

    @PostMapping(value="/mpMsgPush")
    @ApiOperation(value="公众号消息推送_后台接口")
    APIResult mpMsgPush(String unionId, String data) {
        return new APIResult(mpService.sendMpMsg(unionId, data));
    }

}
